package com.feiyangedu.sample;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

public class TCPClient {
    public static void main(String[] args) throws Exception {
        InetAddress address = InetAddress.getLoopbackAddress();
        try (Socket socket = new Socket(address, 9090)){
            try(BufferedReader reader = new BufferedReader(
                    new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8)
            )) {
                try(BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(),
                        StandardCharsets.UTF_8))) {
                    writer.write("time\n");
                    writer.flush();
                    String res = reader.readLine();
                    System.out.println("Response:"+res);
                    Thread.sleep(2000);
                    writer.write("q\n");
                    writer.flush();
                    res = reader.readLine();
                    System.out.println("Response:"+res);
                }

            }
        }
    }
}
